package com.splider.toutiao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinese.converter.Opencc;

public class CommonUtils {
	
	private Logger logger = Logger.getLogger("CommonUtil");
	
	private int words = 10000;
	
	public Opencc opencc = new Opencc("s2twp");
	
	@SuppressWarnings("restriction")
	public String runJSAndReturn(String jsContent,String method) throws ScriptException, NoSuchMethodException{
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("javascript");
		engine.eval(jsContent);
		if (engine instanceof Invocable) {
			Invocable invoke = (Invocable) engine;
			Object obj = invoke.invokeFunction(method);
			return obj.toString();
		}
		return null;
	}
	
	public JSONObject getUrlParam() {
		JSONObject jsonObject = null;
		FileReader reader = null;
		try {
			ScriptEngineManager manager = new ScriptEngineManager();
			ScriptEngine engine = manager.getEngineByName("javascript");
			
			String jsFileName = System.getProperty("user.dir")+File.separator+"toutiao.js"; // 读取js文件

			reader = new FileReader(jsFileName); // 执行指定脚本
			engine.eval(reader);

			if (engine instanceof Invocable) {
				Invocable invoke = (Invocable) engine;
				Object obj = invoke.invokeFunction("getParam");
				jsonObject = JSONObject.parseObject(obj != null ? obj.toString() : null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return jsonObject;
	}
	
	public String encodeHtml(String html){
		if(StringUtils.isEmpty(html)){
			return "";
		}
		html = html.replace("<", "&lt;");
		html = html.replace(">", "&gt;");
		html = html.replace("\"", "&quot;");
		html = html.replace(" ", "&nbsp;");
		html = html.replace("=","&#x3D;");
		html = html.replaceAll("[\t\n\t]", "");
		return html;
	}
	
	public String decodeHtml(String html){
		if(StringUtils.isEmpty(html)){
			return "";
		}
		html = html.replace("&lt;","<");
		html = html.replace("&gt;",">");
		html = html.replace("&quot;","\"");
		html = html.replace("&nbsp;"," ");
		html = html.replace("&#x3D;","=");
		return html;
	}
	
	public String simple2tw(String content){
		//1w个字符调用一次shell
		/*StringBuffer cs = new StringBuffer("");
		int length = content.length();
		int count = length/words;
		int ys = length%words;
		if (count==0) {
			cs.append(sc2tw(content));
		}else{
			for(int i=0;i<count;i++){
				cs.append(sc2tw(content.substring(i*words, (i+1)*words)));
				length = (i+1)*words;
			}
			if(ys>0){
				cs.append(sc2tw(content.substring(length)));
			}
		}
		return cs.toString();*/
		return opencc.convert(content);
	}
	
	public Object sc2tw(String content){
		try {
			StringBuffer sb = new StringBuffer("");
			Process ps = Runtime.getRuntime().exec(ApplicationConfig.prop.getProperty("shell")+" \""+content+"\"");
			BufferedReader in = new BufferedReader(new InputStreamReader(ps.getInputStream()));
			String line = null;
			ps.waitFor();
			while((line = in.readLine())!=null){
				sb.append(line);
			}
			in.close();
			if(sb.indexOf("\"")==0){
				sb = sb.deleteCharAt(0);
			}
			if(sb.lastIndexOf("\"")==sb.length()-1){
				sb = sb.deleteCharAt(sb.length()-1);
			}
			ps.destroy();
			line = null;
			return sb;
		} catch (IOException e) {
//			logger.error("shell执行出错");
		} catch (InterruptedException e) {
//			logger.error("shell执行出错");
		}
		return content;
	}
	/**
	 * 抓取可用的代理IP
	 */
	public List<ProxyIP> getProxyIPList(){
		List<ProxyIP> list = new ArrayList<ProxyIP>();
		/*if(list==null||list.size()==0){
			list = getFreeIPList();
		}*/
		String[] urls = {ApplicationConfig.prop.getProperty("proxy_url")};
	//			String[] urls = {"http://dev.kuaidaili.com/api/getproxy/?orderid=958462473108491&num=600&b_pcchrome=1&b_pcie=1&b_pcff=1&protocol=1&method=2&an_an=1&an_ha=1&quality=1&sort=1&sep=1"};
		list.addAll(getProxyIPs(urls[0]));
		return list;
	}
	
	public List<ProxyIP> getHttpsProxy(ProxyIP proxyIP){
		Map<String, String> header = new HashMap<String, String>();
		header.put("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
		header.put("Accept-Encoding","gzip, deflate, br");
		header.put("Accept-Language","zh-CN,zh;q=0.8");
		header.put("Cache-Control","max-age=0");
		header.put("Connection","keep-alive");
		header.put("Cookie","lang=zh-CN; UM_distinctid=15f991f5e0cf9-0ed3b3ad69b9fd-3b3e5906-1aeaa0-15f991f5e0db7; CNZZDATA1267483031=666948059-1510102493-%7C1510102493; Hm_lvt_b5b6cdc2e21a646a6938ae301388c5e5=1509604913,1510102491,1510102739; Hm_lpvt_b5b6cdc2e21a646a6938ae301388c5e5=1510102743");
		header.put("Host","proxy.coderbusy.com");
		header.put("Referer","https://proxy.coderbusy.com/zh-cn/classical/https-ready.aspx");
		header.put("Upgrade-Insecure-Requests","1");
		header.put("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
		List<ProxyIP> ipList = new ArrayList<ProxyIP>();
		for (int i = 1; i <= 3; i++) {
			Connection conn = Jsoup.connect("https://proxy.coderbusy.com/zh-cn/classical/https-ready/p"+i+".aspx").ignoreContentType(true).headers(header);
			if(proxyIP!=null){
				conn = conn.proxy(proxyIP.getIp(), proxyIP.getPort());
			}
			Document doc = null;
			try {
				doc = conn.get();
			} catch (IOException e) {
//				logger.error("fetch proxy ip error:"+e.getMessage());
			}
			if(doc!=null){
				Elements tbody = doc.getElementsByTag("tbody");
				if(tbody!=null&&tbody.size()>0){
					Elements trs = tbody.get(0).getElementsByTag("tr");
					for (Element tr : trs) {
						Elements td = tr.getElementsByTag("td");
						try {
							ProxyIP ip = new ProxyIP();
							ip.setIp(td.get(0).text().trim());
							ip.setPort(Integer.parseInt(td.get(1).text().trim()));
							ipList.add(ip);
						} catch (Exception e) {
//							e.printStackTrace();
						}
					}
				}
			}
		}
		return ipList;
	}
	
	public List<ProxyIP> getFreeIPList(){
		List<ProxyIP> ipList = new ArrayList<ProxyIP>();
		for (int page = 1; page <= 10; page++) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			Connection conn = Jsoup.connect("http://nyloner.cn/proxy?page="+page+"&num=15"+getTokenParam(page, 15)).ignoreContentType(true);
			Document doc = null;
			try {
				doc = conn.get();
			} catch (IOException e) {
				logger.error("fetch proxy ip error:"+e.getMessage());
			}
			if(doc!=null){
				String json = doc.body().text();
				JSONObject jsonObject = JSONObject.parseObject(json);
				JSONArray list = jsonObject.getJSONArray("list");
				if(list!=null&&list.size()>0){
					for (int i = 0; i < list.size(); i++) {
						try {
							ProxyIP ip = new ProxyIP();
							ip.setIp(list.getJSONObject(i).getString("ip"));
							ip.setPort(list.getJSONObject(i).getIntValue("port"));
							ipList.add(ip);
						} catch (Exception e) {
						}
					}
				}
			}
		}
		return ipList;
	}
	
	public List<ProxyIP> getKuaiDaiLi(){
		List<ProxyIP> ipList = new ArrayList<ProxyIP>();
		for (int page = 1; page <= 10; page++) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			Connection conn = Jsoup.connect("http://www.kuaidaili.com/proxylist/"+page).ignoreContentType(true);
			Document doc = null;
			try {
				doc = conn.get();
			} catch (IOException e) {
				logger.error("fetch proxy ip error:"+e.getMessage());
			}
			if(doc!=null){
				Elements tbody = doc.getElementsByTag("tbody");
				if(tbody!=null&&tbody.size()>0){
					Elements trs = tbody.get(0).getElementsByTag("tr");
					if(trs!=null&&trs.size()>0){
						for (Element tr : trs) {
							Elements td = tr.getElementsByTag("td");
							if(td!=null&&td.size()>0){
								try {
									ProxyIP ip = new ProxyIP();
									ip.setIp(td.get(0).text().trim());
									ip.setPort(Integer.parseInt(td.get(1).text().trim()));
									ipList.add(ip);
								} catch (Exception e) {
								}
							}
						}
					}
				}
			}
		}
		return ipList;
	}
	
	public List<ProxyIP> getProxyIPs(String url){
		List<ProxyIP> ipList = new ArrayList<ProxyIP>();
		Connection con = Jsoup.connect(url).timeout(10000)
				.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");
		try {
			Document doc = con.get();
			Elements body = doc.getElementsByTag("body");
			if(body!=null&&body.size()>0){
				Element b = body.get(0);
				String ips = b.text();
				if(StringUtils.isNotEmpty(ips)&&!ips.toLowerCase().contains("error")){
					String[] iparray = ips.split(" ");
					for(String ip : iparray){
						ip = ip.replace("：", ":");
						ProxyIP proxyIP = new ProxyIP();
						proxyIP.setIp(ip.split(":")[0]);
						proxyIP.setPort(Integer.parseInt(ip.split(":")[1]));
						ipList.add(proxyIP);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ipList;
	}
	
	public String getTokenParam(int page,int num){
		int timestamp = (int) (System.currentTimeMillis()/1000);
		return "&token="+MD5Util.MD5Str(page+""+num+timestamp)+"&t="+timestamp;
	}
}
